
from django.shortcuts import render, HttpResponse
import random
from utils.tencent.sms import send_sms_single
from django.conf import settings
from django.shortcuts import HttpResponse
from django_redis import get_redis_connection
from app01.forms import RegisterForm
from django.urls import reverse
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError

def send_sms(request,tpl,mobile_phone):
    """ 发送短信
        ?tpl=login  -> 548762
        ?tpl=register -> 548760
    """
    # tpl = request.GET.get('tpl')
    template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
    if not template_id:
        return HttpResponse('模板不存在')

    code = random.randrange(1000, 9999)
    # 注意code里面传的是两个参数
    res = send_sms_single( mobile_phone,template_id, [code,10 ])
    if res['result'] == 0:
        return ['成功',code,mobile_phone]
    else:
        return HttpResponse(res['errmsg'])


def register(request):
    form = RegisterForm()
    conn = get_redis_connection("default")
    if request.method == 'POST':
        tpl = request.POST.get('tpl')
        mobile_phone = request.POST.get('phone')
        ret = send_sms(request,tpl,mobile_phone)

    return render(request, 'app01/register.html', {'form': form})




def index(request):

    conn = get_redis_connection("default")
    conn.set('zmy',"张明阳",ex=10)
    value = conn.get('zmy')
    print(str(value,encoding='utf-8'))

    return HttpResponse('OK')

from django.shortcuts import render

# Create your views here.

